package com.teach.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teach.dto.SeckillGoodsDto;
import com.teach.entity.ResponseEntity;
import com.teach.entity.TbDraw;
import com.teach.entity.TbSeckillgoods;
import com.teach.mapper.TbDrawMapper;
import com.teach.mapper.TbSeckillgoodsMapper;
import com.teach.service.TbSeckillgoodsService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/*import com.teach.entity.ResponseEntity;
import com.teach.entity.TbDraw;
import com.teach.entity.TbSeckillgoods;*/

@Service
public class TbSeckillgoodsServiceImpl extends ServiceImpl<TbSeckillgoodsMapper, TbSeckillgoods>
    implements TbSeckillgoodsService {
    @Resource
    private TbSeckillgoodsMapper tbSeckillgoodsMapper;
    @Resource
    TbDrawMapper tbDrawMapper;

    //日志
    Logger logger= LogManager.getLogger();

    /**
     * 添加秒杀表
     * @param seckillGoodsDto
     * @return
     */
    @Override
    public ResponseEntity addSeckill(SeckillGoodsDto seckillGoodsDto) {
        TbSeckillgoods seckillgoods = new TbSeckillgoods();
        List<TbSeckillgoods> tbSeckillgoods = seckillGoodsDto.getTbSeckillgoods();
        for (TbSeckillgoods tbSeckillgood : tbSeckillgoods) {
            seckillgoods.setNum(tbSeckillgood.getNum());
            seckillgoods.setCoursePrice(tbSeckillgood.getCoursePrice());
            seckillgoods.setQuantity(tbSeckillgood.getQuantity());
            seckillgoods.setCourseSynopsis(tbSeckillgood.getCourseSynopsis());
            seckillgoods.setStatus(0);
            seckillgoods.setCouponStatus(tbSeckillgood.getCouponStatus());
            seckillgoods.setCourseName(tbSeckillgood.getCourseName());
            seckillgoods.setCouponMoney(tbSeckillgood.getCouponMoney());
            seckillgoods.setStartTime(LocalDateTime.now());
            seckillgoods.setEndTime(LocalDateTime.now());
            //添加秒杀表
            tbSeckillgoodsMapper.insert(seckillgoods);
        }
        return new ResponseEntity(null,200,"添加成功",true);
    }

    /**
     * 修改审核状态
     * @param seckillGoodsDto
     * @return
     */
    @Override
    public ResponseEntity updateState(SeckillGoodsDto seckillGoodsDto) {
        TbSeckillgoods seckillgoods = new TbSeckillgoods();
        TbSeckillgoods dtoSeckillgoods = seckillGoodsDto.getSeckillgoods();
        try {
            seckillgoods.setId(dtoSeckillgoods.getId());
            seckillgoods.setStatus(1);
            tbSeckillgoodsMapper.updateById(seckillgoods);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("审核状态："+e.getMessage());
            return new ResponseEntity(null,500,"审核修改失败",false);
        }
        return new ResponseEntity(null,200,"审核修改成功",true);
    }

    @Override
    public ResponseEntity addDraw(Integer id, String token) {
        //解析token
        Claims claims = Jwts.parser().setSigningKey("huangyujie").parseClaimsJws(token).getBody();
        Integer uid=Integer.parseInt(claims.getId());
        //根据课程id查询
        TbSeckillgoods seckillgoods = tbSeckillgoodsMapper.selectById(id);
        TbDraw tbDraw = new TbDraw();
        tbDraw.setPrice(new BigDecimal(seckillgoods.getCouponMoney()));
        tbDraw.setName(seckillgoods.getCouponMoney()+"优惠券");
        tbDraw.setCid(id);
        tbDraw.setUid(uid);
        tbDraw.setStartTime(LocalDateTime.now());
        tbDraw.setEndTime(LocalDateTime.now());
        tbDraw.setClaimTime(LocalDateTime.now());
        //添加
        tbDrawMapper.insert(tbDraw);
        return new ResponseEntity(null,200,"添加成功",true);
    }

}




